from fastapi import FastAPI,HTTPException,status
import bean.param as param

import time

# 三个模块包装的py
import pred_model
import cae_model
import measure_model
import uvicorn

import my_logger

app=FastAPI()



@app.post("/analysis")
async def analysis(data:param.AnalysisReq):

    try:
        
        my_logger.log_message("\nanalysis,param:%s\n",data)

        allstartTime=time.time()

        # 处理 神经网络模块
        model_time = time.time()
        predOutPut= pred_model.analysis(data, data.preEvaluate)

        my_logger.log_message("\n----------神经网络分析 End----------"
                              "\n分析耗时：%ss \npredOutPut:%s", str(time.time()-model_time),str(predOutPut))

        # 处理 有限元 模块  图片列表和数据
        model_time = time.time()
        pic_info_list, caeOutPut, bridge_info_dict = cae_model.analysis(data, data.cae)
        my_logger.log_message("\n----------有限元分析 End----------"
                              "\n分析耗时：%ss， \ncaeOutPut:%s", str(time.time() - model_time), str(caeOutPut))

        # 处理 数据分析模块(包含综合研判)
        model_time = time.time()
        measureOutPut = measure_model.analysis(data,data.measure, predOutPut, caeOutPut, bridge_info_dict)
        my_logger.log_message("\n----------数据分析 End----------"
                              "\n分析耗时：%ss， \ncaeOutPut:%s\n", str(time.time() - model_time), str(measureOutPut))

        # 返回的数据，只有综合研判的，以及对应的图片地址即可
        my_logger.log_message("\n----------全部分析 End----------"
                              "\n总耗时：%ss\n\n", str(time.time() - allstartTime))

        # my_logger.log_error('\nlogger数量测试：')  # 测试logger数量

        list1= measureOutPut["振型阶数"]
        list2= measureOutPut["固有频率(Hz)"]
        list3= measureOutPut["振型阻尼比"]
        list4= measureOutPut["模态质量(t)"]


        resultList=[]
        for i in range(len(list1)):
            resultList.append({"zxjs":str(list1[i]),"gypl":str(round(list2[i],3)),"zxznb":str(round(list3[i],5)),"mtzl":str(list4[i]),})


        return {"code":2000,"data":{"resultList":resultList,"imageList":pic_info_list}}
    
    except Exception as e:
        print(e)
        my_logger.log_error(str(e),str(data.recordId))
        return {"code":5000,"message":str(e)}




if __name__ == "__main__":
    uvicorn.run("app_main:app",port=9094,reload=False)
